//
//  MyAppDelegate.m
//  jRSSReader
//
//  Created by Jodynek on 4/12/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MyAppDelegate.h"
#import "frmSettingsController.h"

@implementation MyAppDelegate

@synthesize window = _window;

- (void)resizeWindowToSize:(NSRect)newFrame
{
  [_window setFrame:newFrame display:YES animate:YES];
}

- (void) applicationWillTerminate:(NSNotification *)notification
{
  NSRect aFrame;
  aFrame = [NSWindow contentRectForFrameRect:[_window frame]
                                   styleMask:[_window styleMask]];
  NSValue *value = [NSValue valueWithRect:aFrame];  
  NSData *location = [NSKeyedArchiver archivedDataWithRootObject:value];
  NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  [userDefaults setObject:location forKey:@"windowsrect"];
  [userDefaults synchronize];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
<<<<<<< HEAD
  // allow only one instance at the sametime
  if ([[NSRunningApplication runningApplicationsWithBundleIdentifier:[[NSBundle mainBundle] bundleIdentifier]] count] > 1) 
  {
    [NSApp terminate:nil];
  }
=======
>>>>>>> 42acf678065da260e0e994d7eaf924250eff120f
  // load window size and position
  NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
  if (prefs != nil)
  {
    NSRect screenRect;
    NSData *data = [prefs objectForKey:@"windowsrect"];
    if (data != nil) 
    {
      NSValue *unarchived = [NSKeyedUnarchiver unarchiveObjectWithData:data];
      screenRect = unarchived.rectValue;
      [self resizeWindowToSize:screenRect];
    } else {
      screenRect.origin.x = 100;
      screenRect.origin.y = 80;
      screenRect.size.width = 800;
      screenRect.size.height = 70;  
      [self resizeWindowToSize:screenRect];
    }
  } else {
    // resize window
    NSRect screenRect;
    NSScreen *screen = [[NSScreen screens] objectAtIndex: 0];
    screenRect = [screen visibleFrame];    
    NSRect windowRect;
    int newWinHeight = 70; // Vertical height
    int newWinWidth = screenRect.size.width; // Horizontal width
    windowRect = NSMakeRect([_window frame].origin.x - (newWinWidth - (int)(NSWidth([_window frame]))), [_window frame].origin.y - (newWinHeight - (int)(NSHeight([_window frame]))), newWinWidth, newWinHeight);
    [_window setFrame:windowRect display:YES animate:NO];     
    // move window to the bottom of the screen
    NSPoint pos;
    pos.x = screenRect.origin.x;
    pos.y = screenRect.origin.y;
    [_window setFrameOrigin:pos]; 
  }
<<<<<<< HEAD
  // remove application icon from dock bar
//  ProcessSerialNumber psn = { 0, kCurrentProcess };
//  TransformProcessType(&psn, kProcessTransformToUIElementApplication);
=======
  
>>>>>>> 42acf678065da260e0e994d7eaf924250eff120f
  [_window makeKeyAndOrderFront:nil];
//  TransformProcessType(&psn, kProcessTransformToForegroundApplication);
}

@end
